package message.sender;

import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import message.read.Read;

/**
 * @ClassName Sender
 * @Description TODO
 * @Author niu-xi
 * @Date 2020年6月5日 下午9:07:03
 * @Version 1.0
 */
public class Sender {

	
	private static final String USERNAME = ActiveMQConnection.DEFAULT_USER; // 默认的连接用户名
	private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; // 默认的连接密码
	private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL; // 默认的连接地址
	private static final int SENDNUM = 11; // 发送的消息数量
	ConnectionFactory cn = null;
	Connection connection = null;
	Session session = null;
	Destination destination = null;
	
	/**
	 * build connection through ActiveMQConnectionFactory
	 */
	public void init() {
		cn = new ActiveMQConnectionFactory(Sender.USERNAME, Sender.PASSWORD,Sender.BROKEURL);// 调用activeMq工厂JMS通过这创建连接
		try {
			connection = cn.createConnection();// build connect between producer and customers
			connection.start();
		} catch (JMSException e) {
			e.printStackTrace();
		}

	}
	
	/**
	 * 
	 */
	public void produce() {
		try {
			MessageProducer messageProducer;
			session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);// 启动会话，true代表自动开启事务
			destination = session.createQueue("Queue2");// 创建队列接sender到receiver的信息
			messageProducer = session.createProducer(destination);// build producer
			Read readtxt = new Read();
			List<String> list = readtxt.readFile();// read file information
			int indexOfList = 0;
			for (int i = 0; i < Sender.SENDNUM; i++) {
				if (indexOfList >= list.size()) {
					break;
				}
				TextMessage message = session.createTextMessage(list.get(indexOfList++));// 读取保存excel内容的list的各个字符串元素到消息体中，同时把下标加一以便读取下个内容
				System.out.println(Thread.currentThread().getName() + "线程" + "发送消息：" + "ActiveMQ 发布的消息" + ":" + i);//测试试生产者有没有读取信息
				messageProducer.send(message);//send to customers
			}
			session.commit();
			messageProducer.close();
			session.close();
			connection.close();
		} catch (JMSException e) {
			try {
				session.rollback();
			} catch (JMSException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			e.printStackTrace();
		}
	}

	public static void sendMessage(Session session, MessageProducer messageProducer) throws Exception {
		for (int i = 0; i < Sender.SENDNUM; i++) {
			TextMessage textmessage = session.createTextMessage("ActiveMQ" + i);
			System.out.println("ActiveMQ sends" + i);
			messageProducer.send(textmessage);
			session.commit();
		}
	}
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		/**
		 * 3 threads to execute 5 tasks
		 */
		ExecutorService threadPool = Executors.newFixedThreadPool(3);
		for (int i = 0; i < 1; i++) {     
			threadPool.submit(new Runnable() {

				@Override
				public void run() {
					Sender s = new Sender();
					try {
						Thread.sleep(new Random().nextInt(5) * 500);
					} catch (Exception e) {
						e.printStackTrace();
					}
					s.init();// buile connect
					s.produce();
				}
			});
		}
	}
}
